<?php
require('./constrl/lookPage.php');
pdlook();
require('../model/conn.php');
// 处理升级为管理员请求
if (isset($_GET['upgradeUid'])) {
  $uid = intval($_GET['upgradeUid']);
  // 检查是否存在 is_admin 字段
  $colCheck = exec_select("SHOW COLUMNS FROM tbl_user LIKE 'is_admin'");
  if (count($colCheck) == 0) {
    // 如果没有 is_admin 字段，则添加该字段（模拟升级功能）
    exec_update("ALTER TABLE `tbl_user` ADD `is_admin` TINYINT(1) DEFAULT 0");
  }
  // 将指定用户升级为管理员 (设置 is_admin = 1)
  $sql = "UPDATE tbl_user SET is_admin=1 WHERE uid=$uid";
  $rows = exec_update($sql);
  if ($rows > 0) {
    echo "<script>alert('用户已升级为管理员');window.location.href='user_upgrade.php';</script>";
    exit;
  } else {
    echo "<script>alert('升级失败或用户已是管理员');window.location.href='user_upgrade.php';</script>";
    exit;
  }
}
// 获取用户列表（包含管理员标识）
$hasAdminCol = false;
$colCheck = exec_select("SHOW COLUMNS FROM tbl_user LIKE 'is_admin'");
if (count($colCheck) > 0) {
  $hasAdminCol = true;
  $users = exec_select("SELECT uid, uName, created_time, is_admin FROM tbl_user");
} else {
  $users = exec_select("SELECT uid, uName, created_time FROM tbl_user");
}
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>说点吧 - 账户升级</title>
  <link rel="stylesheet" href="../style/css/bootstrap.min.css">
  <link rel="stylesheet" href="../style/css/adminIndex.css">
  <link rel="stylesheet" href="../style/css/font-face.css">

</head>

<body>
  <!-- 导航栏 -->
  <nav class="navbar navbar-default">
    <div class="container-fluid">
      <div class="navbar-header">
        <span class="navbar-brand">说点吧 后台管理</span>
      </div>
      <ul class="nav navbar-nav">
        <li><a href="index.php">后台首页</a></li>
        <li><a href="user_manage.php">用户管理</a></li>
        <li class="active"><a href="user_upgrade.php">账户升级</a></li>
        <li><a href="board_manage.php">板块管理</a></li>
        <li><a href="article_manage.php">管理文章</a></li>
        <li>
          <?php
          if (isset($_SESSION['hdb']) && trim($_SESSION['hdb']) != '') {
            echo "<span> $_SESSION[hdb]</span><a href='view/adminOut.php'>退出管理</a>";
          }
          ?>
        </li>
      </ul>
    </div>
  </nav>

  <div class="container">
    <h2>账户升级</h2>
    <!-- 用户列表表格 -->
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>用户ID</th>
          <th>用户名</th>
          <th>注册时间</th>
          <?php if ($hasAdminCol) { ?>
            <th>是否管理员</th>
          <?php } ?>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($users as $user) {
          // 判断当前用户是否管理员
          $isAdmin = ($hasAdminCol && isset($user['is_admin'])) ? $user['is_admin'] : 0;
          ?>
          <tr>
            <td><?php echo $user['uid']; ?></td>
            <td><?php echo $user['uName']; ?></td>
            <td><?php echo $user['created_time']; ?></td>
            <?php if ($hasAdminCol) { ?>
              <td><?php echo $isAdmin ? "是" : "否"; ?></td>
            <?php } ?>
            <td>
              <?php if ($isAdmin) { ?>
                <button class="btn btn-sm btn-default" disabled>已是管理员</button>
              <?php } else { ?>
                <a href="?upgradeUid=<?php echo $user['uid']; ?>" class="btn btn-sm btn-warning"
                  onclick="return confirm('确定将该用户升级为管理员吗？');">升级为管理员</a>
              <?php } ?>
            </td>
          </tr>
        <?php } ?>
      </tbody>
    </table>
  </div>

  <script src="../style/js/jquery.min.js"></script>
  <script src="../style/js/bootstrap.min.js"></script>
</body>

</html>